﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iOak.Domain.Campaigns;
using iOak.Common.Untity;
using iOak.Common;
using iOak.Business.Campaigns;
using iOak.Web.ExeclHelper;

namespace iOak.Web.Administration
{
    public partial class CampaignHistoryList : BasePage
    {
        private WorkAssistant _workassistant = new WorkAssistant();
        private CampaignService _campaignService = new CampaignService();
        private ApplicationService _applicationService = new ApplicationService();
        private ApplicationCandidateService _applicationCandidateService = new ApplicationCandidateService();

        #region 页面事件
        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            pagerControl.PageIndexChange += new Modules.PagerControl.OnPageIndexChangeHandler(pagerControl_PageIndexChange);
            if (!Page.IsPostBack)
            {
                BindCampaignList();
            }
        }

        protected void pagerControl_PageIndexChange(object serder, EventArgs e)
        {
            BindCampaignList();
        }

        /// <summary>
        /// gvCampaign行绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvCampaign_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Campaign campaign = e.Row.DataItem as Campaign;
                Label lblReceiveCount = e.Row.FindControl("lblReceiveCount") as Label;
                //总申请书个数
                int applicationCount = _applicationService.GetCountByCampaignId(campaign.CampaignId);
                //未申报的申请书个数
                int openCount = _applicationService.GetCountByCampaignId(campaign.CampaignId, ApplicationStatus.Open);
                //收到的个数
                lblReceiveCount.Text = (applicationCount - openCount).ToString();
                Label lblCandidateCount = e.Row.FindControl("lblCandidateCount") as Label;
                lblCandidateCount.Text = _applicationCandidateService.GetCount(campaign.CampaignId, includeOpen: false).ToString();
                Label lblMeetCount = e.Row.FindControl("lblMeetCount") as Label;
                lblMeetCount.Text = _applicationCandidateService.GetCount(campaignId: campaign.CampaignId, passed: true).ToString();
                //设置操作按钮
                Panel phOpenMenu = e.Row.FindControl("pnlOptionMenu") as Panel;
            }
        }

        /// <summary>
        /// 检索按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFind_Click(object sender, EventArgs e)
        {
            gvCampaign.PageIndex = 0;
            BindCampaignList();
        }

        protected void btnPostBack_Click(object sender, EventArgs e)
        {
            Guid id;
            switch (hidEventName.Value)
            {
                case "export":
                    id = Guid.Parse(hidEventArguments.Value);
                    ExportManager.CreateSumExecl(id);
                    break;

            }
        }
        #endregion

        #region 处理函数
        /// <summary>
        /// 绑定Campaign列表
        /// </summary>
        private void BindCampaignList()
        {
            DateTime tempDate;
            DateTime? startDate = null;
            if (!String.IsNullOrWhiteSpace(txtStartDate.Text))
            {
                if (DateTime.TryParse(txtStartDate.Text, out tempDate))
                {
                    startDate = tempDate;
                }
                else
                {
                    txtStartDate.Text = String.Empty;
                }
            }
            DateTime? endDate = null;
            if (!String.IsNullOrWhiteSpace(txtEndDate.Text))
            {
                if (DateTime.TryParse(txtEndDate.Text, out tempDate))
                {
                    endDate = tempDate;
                }
                else
                {
                    txtEndDate.Text = String.Empty;
                }
            }
            int? status = (int)CampaignStatus.Closed;
            CampaignService campaignService = new CampaignService();
            //检索符合条件的campaign
            List<Campaign> campaignList = campaignService.FindCampaigns(txtSubject.Text, startDate, endDate, status);
            pagerControl.BindData(gvCampaign, campaignList.OrderByDescending(item => item.StartDate).ToList());
        }
        #endregion
    }
}